<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryForm" :inline="true">
        <!-- StartDate  EndDate -->
        <el-form-item prop="keywords">
          <el-input v-model="queryForm.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <el-row :gutter="20">
        <el-col :span="24" :offset="0" style="margin-top: 10px">
          <el-table
            ref="tableRef"
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            :show-overflow-tooltip="{ effect: 'light' }"
          >
            <el-table-column label="指挥部名称" prop="name" min-width="150" />
            <el-table-column label="所属建设单位" prop="constructorName" min-width="200" />
            <el-table-column label="指挥者" prop="leader" min-width="120" />
            <el-table-column label="联系电话" prop="mobile" min-width="120" />
            <el-table-column label="指挥者身份证号" prop="cardno" min-width="120" />
            <el-table-column label="操作" fixed="right" width="80">
              <template #default="scope">
                <el-button
                  v-hasPerm="['corp:commander:edit']"
                  type="primary"
                  link
                  size="small"
                  @click="handelEdit(scope.row)"
                >
                  编辑
                </el-button>
                <el-button
                  v-hasPerm="['corp:commander:delete']"
                  type="primary"
                  link
                  size="small"
                  @click="handelDelete(scope.row)"
                >
                  删除
                </el-button>
                <el-button
                  v-hasPerm="['corp:commander:resetpassword']"
                  type="primary"
                  link
                  size="small"
                  @click="handelResetPassword(scope.row)"
                >
                  重置密码
                </el-button>
                <el-button v-if="permissions.detail" type="primary" link size="small" @click="handelDetail(scope.row)">
                  详情
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryForm.pageIndex"
            v-model:limit="queryForm.pageSize"
            @pagination="handleQuery"
          />
        </el-col>
      </el-row>
    </div>
    <edit-form v-if="dialogAddOrEdit.visible" :dialog-add-or-edit="dialogAddOrEdit" @send-close="sendClose" />
    <detail :dialog-detail="dialogDetail" @send-close="sendClose" />
  </div>
</template>
<script setup lang="ts">
import PasswordInput from "@/components/PasswordInput/index.vue";
import detail from "./detail.vue";
import editForm from "./editForm.vue";
import { QueryCommanderPageList, deleteCommander, ResetPassword } from "@/api/admin/enterprise/index";

const permissions = ref({
  detail: true,
});

const loading = ref(false);
const tableData = ref();

const dialogDetail = reactive<DialogOption>({
  visible: false,
});
const dialogAddOrEdit = reactive<DialogOption>({
  visible: false,
  title: "新增指挥部信息",
  footBtn: "新增",
  id: 0,
});
const tableRef = ref();

//详情
const handelDetail = function (row: any) {
  dialogDetail.visible = true;
  dialogDetail.id = row.commanderId;
  dialogDetail.title = "详情";
};

const sendClose = function (needRefresh: boolean) {
  dialogDetail.visible = false;
  dialogDetail.id = undefined;
  dialogAddOrEdit.visible = false;
  dialogAddOrEdit.id = undefined;
  if (needRefresh) {
    handleQuery();
  }
};

/**
 * 编辑
 */
const handelEdit = function (row: any) {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.id = row.commanderId;
  dialogAddOrEdit.title = "编辑";
  dialogAddOrEdit.footBtn = "确认修改";
};

const handelDelete = function (row: any) {
  ElMessageBox.confirm("确定删除该指挥部信息吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    loading.value = true;
    deleteCommander(row.commanderId)
      .then(
        (res) => {
          if (res.type === 0) {
            if (res.success) {
              ElMessage.success(res.msg);
              handleQuery();
            } else {
              ElMessage.warning(res.msg);
            }
          } else {
            ElMessage.warning(res.msg);
          }
        },
        (err) => {
          ElMessage.warning(err.msg);
        }
      )
      .finally(() => {
        loading.value = false;
      });
  });
};

/**
 * 重置密码
 */
const passwordForm = ref({
  password: "",
});
const validatePassword = ref<boolean>(false);
const handelResetPassword = function (row: any) {
  ElMessageBox({
    title: "确定重置该指挥部密码吗？",
    message: () =>
      h("div", [
        h(
          ElForm,
          {
            model: passwordForm,
            labelWidth: "80px",
          },
          [
            h(
              ElFormItem,
              {
                rules: [
                  {
                    required: true,
                    message: "请输入密码",
                  },
                ],
                label: "密码",
                prop: "password",
              },
              [
                h(PasswordInput, {
                  onInputChange: (value: string, allow: boolean) => {
                    passwordForm.value.password = value;
                    validatePassword.value = allow;
                  },
                }),
              ]
            ),
          ]
        ),
      ]),
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    showCancelButton: true,
    beforeClose(action, instance, done) {
      if (action === "confirm") {
        if (!validatePassword.value) return;
        instance.confirmButtonLoading = true;
        instance.confirmButtonText = "执行中...";
        ResetPassword(row.commanderId, passwordForm.value.password)
          .then((res) => {
            if (res.success) {
              ElMessage({
                type: "success",
                message: res.msg,
              });
              instance.confirmButtonLoading = false;
              instance.confirmButtonText = "确定";
              done();
            } else {
              ElMessage({
                type: "error",
                message: res.msg,
              });
            }
          })
          .finally(() => {
            loading.value = false;
          });
      } else {
        done();
      }
    },
  });
};

//查询条件
const queryForm = reactive({
  keywords: "",
  pageIndex: 1,
  pageSize: 10,
});
const total = ref(0);
//这是列表数据
const handleQuery = function () {
  loading.value = true;
  QueryCommanderPageList(queryForm)
    .then((res) => {
      if (res.type === 0) {
        if (res.success) {
          tableData.value = res.data.list;
          queryForm.pageIndex = res.data.pageNum;
          queryForm.pageSize = res.data.pageSize;
          total.value = res.data.total;
        } else {
        }
      } else {
      }
    })
    .finally(() => {
      loading.value = false;
    });
};
onMounted(() => {
  handleQuery();
});
</script>
<style scoped>
:deep(.el-button) {
  margin-left: 5px;
}
:deep(.el-input__wrapper) {
  border-radius: 0;
}
</style>
